package logic

import (
	"context"
	"errors"
	"houseRpc/models"

	"houseRpc/house"
	"houseRpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ShowHouseLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewShowHouseLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ShowHouseLogic {
	return &ShowHouseLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ShowHouseLogic) ShowHouse(in *house.ShowHouseRequest) (*house.ShowHouseResponse, error) {
	// todo: add your logic here and delete this line

	var err error
	var h models.House
	var hs models.HouseInfo
	h.ID = uint(in.HouseId)
	hs.HouseId = int(in.HouseId)

	if err = h.GetHouseById(); err != nil {
		logx.Error("房源信息获取失败")
		return nil, errors.New("房源信息获取失败")
	}
	if h.ID == 0 {
		logx.Error("房源不存在")
		return nil, errors.New("房源不存在")
	}
	if err = hs.GetHouseInfoById(); err != nil {
		logx.Error("房源详细信息获取失败")
		return nil, errors.New("房源详细信息获取失败")
	}

	return &house.ShowHouseResponse{
		HouseId:   in.HouseId,
		Image:     hs.Image,
		Title:     h.Title,
		Ads:       h.Ads,
		Square:    h.Square,
		Around:    hs.Around,
		PriceInfo: hs.PriceInfo,
	}, nil
}
